programming4us
           
 
 
Programming

Parallel Programming with Microsoft .Net : Parallel Tasks - Anti-Patterns

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/28/2010 2:42:11 PM

1. Variables Captured by Closures

In C#, a closure can be created with a lambda expression in the form args => body that represents an unnamed (anonymous) delegate. A unique feature of closures is that they may refer to variables defined outside their lexical scope, such as local variables that were declared in a scope that contains the closure.

The semantics of closures in C# may not be intuitive to some programmers and it’s easy to make mistakes. Unless you understand the semantics, you may find that captured variables don’t behave as you expect, especially in parallel programs.

Problems occur when you reference a variable without considering its scope. Here’s an example.

for (int i = 0; i < 4; i++)
{
// WARNING: BUGGY CODE, i has unexpected value
Task.Factory.StartNew(() => Console.WriteLine(i));
}


Note:

Capturing a loop index variable in a closure is usually a bug. Be on the lookout for this very common coding error.


You might think that this code sample would print the numbers 1, 2, 3, 4 in some arbitrary order, but it can print other values, depending on how the threads happen to run. For example, you might see 4, 4, 4, 4. The reason is that the variable i is shared by all the closures created by the steps of the for loop. By the time the tasks start, the value of the single, shared variable i will probably be different from the value of i when the task was created. All the tasks are sharing the same variable.

The solution is to introduce an additional temporary variable in the appropriate scope.

for (int i = 0; i < 4; i++)
{
var tmp = i;
Task.Factory.StartNew(() => Console.WriteLine(tmp));
}

This version prints the numbers 1, 2, 3, 4 in an arbitrary order, but each number will be printed. The reason is that the variable tmp is declared within the block scope of the for loop’s body. This causes a new variable named tmp to be instantiated with each iteration of the for loop. (In contrast, all iterations of the for loop share a single instance of the variable i.)

This bug is one reason why you should use Parallel.For instead of coding a loop yourself. It’s also one of the most common mistakes made by programmers who are new to tasks.

2. Disposing a Resource Needed by a Task

When you create a task, don’t forget that you can’t call the Dispose method on the objects that the task needs to do its work. Careless use of the C# using keyword is a common way to make this mistake. Here’s an example.


Note:

Be careful not to dispose resources needed by a pending task.


Task<string> t;
using (var file = new StringReader("text"))
{
t = Task<string>.Factory.StartNew(() => file.ReadLine());
}
// WARNING: BUGGY CODE, file has been disposed
Console.WriteLine(t.Result);

The using keyword introduces an implicit try/finally block that invokes the Dispose method on the value of the variable when it exits its scope. The using keyword can’t be used with captured variables. Instead, you need to call the Dispose method when you know that the disposable object is no longer needed. In this example, Dispose can only be called after the task’s Result property is read.

3. Avoid Thread Abort

Terminating tasks with the Thread.Abort method leaves the App Domain in a potentially unusable state. Also, aborting a thread pool worker thread is never recommended.


Note:

Never attempt to cancel a task by calling the Abort method of the thread that is executing the task.


Other -----------------
- Parallel Programming with Microsoft .Net : Parallel Tasks - Variations (part 2)
- Parallel Programming with Microsoft .Net : Parallel Tasks - Variations (part 1)
- Parallel Programming with Microsoft .Net : Parallel Tasks - An Example
- Parallel Programming with Microsoft .Net : Parallel Tasks - The Basics
- jQuery 1.3 : The jQuery UI plugin library
- jQuery 1.3 : The Form plugin
- jQuery 1.3 : How to use a plugin
- jQuery 1.3 : Sharing a plugin with the world
- Auditing an Existing Site to Identify SEO Problems (part 3) - Fixing an Internal Linking Problem
- Auditing an Existing Site to Identify SEO Problems (part 2) - The Importance of Keyword Reviews
- Auditing an Existing Site to Identify SEO Problems (part 1 - Elements of an Audit
- First Stages of SEO : Defining Your Site’s Information Architecture
- First Stages of SEO : The Major Elements of Planning
- Understanding Your Audience and Finding Your Niche
- Developing an SEO Plan Prior to Site Development
- Setting SEO Goals and Objectives
- jQuery 1.3 : Developing plugins - Adding a selector expression
- jQuery 1.3 : Developing plugins - Method parameters
- The LINQ Set Operators
- iPhone 3D Programming : Vertices and Touch Points - Creating a Wireframe Viewer (part 3)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us